<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>2.对props进行限制</title>
    <!-- 引入react核心库 -->
    <script src="../React-js/react.development.js"></script>
    <!-- 引入react-dom,用于支持react操作DOM -->
    <script
      type="text/javascript"
      src="../React-js/react-dom.development.js"
    ></script>
    <!-- 引入babel,用于将jsx转换为js -->
    <script type="text/javascript" src="../React-js/babel.min.js"></script>
    <!-- 引入 prop-types 用于对组件标签属性进行限制 -->
    <script type="text/javascript" src="../React-js/prop-types.js"></script>
  </head>
  <body>
    <!-- 准备好一个“容器” -->
    <div id="test1"></div>
    <div id="test2"></div>
    <div id="test3"></div>

    <script type="text/babel">
      // 创建组件
      class Person extends React.Component {
        render() {
          // console.log(this);
          const { name, sex, age } = this.props;
          // props 是只读的
          return (
            <ul>
              <li>姓名:{name}</li>
              <li>性别:{sex}</li>
              <li>年龄:{age + 1}</li>
            </ul>
          );
        }
      }
      // Person.属性规则
      // 对标签属性进行类型,必要性的限制
      Person.propTypes = {
        name: PropTypes.string.isRequired,
        sex: PropTypes.string,
        age: PropTypes.number,
        speak: PropTypes.func,
      };
      // 指定标签的默认属性值
      Person.defaultProps = {
        sex: '男--默认',
        age: 22,
      };
      // 2.渲染组件到页面
      ReactDOM.render(
        <Person name="哈哈" age={24} sex="女" />,
        document.getElementById('test1')
      );
      ReactDOM.render(
        <Person name="cxh" age={23} />,
        document.getElementById('test2')
      );

      const p = { name: 'xx', age: 22, sex: '女' };
      ReactDOM.render(<Person {...p} />, document.getElementById('test3'));

      function speak() {
        console.log('I can speak');
      }
    </script>
  </body>
</html>
